package com.zzuli.yxy.controller;

import com.zzuli.yxy.entity.Role;
import com.zzuli.yxy.enumeration.AppHttpCodeEnum;
import com.zzuli.yxy.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zzuli.yxy.service.RoleService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @auther pony
 * @create 2023-03-28 15:41
 */
@RestController
@RequestMapping("role")
@CrossOrigin(origins = "*")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取全部角色信息
     * @return
     */
    @GetMapping("getAllRole")
    public R getAllRole(){
        return roleService.getAllRole();
    }

    /**
     * 获取角色管理分页信息
     * @param pageCurrent 当前页
     * @param pageSize 每页条数
     * @param name 关键字 非必传
     * @return
     */
    @GetMapping({"/page/{pageCurrent}/{pageSize}",
            "/page/{pageCurrent}/{pageSize}/{name}"})
    public R getPage(@PathVariable("pageCurrent") Integer pageCurrent,
                     @PathVariable("pageSize") Integer pageSize,
                     @PathVariable(name = "name", required = false) String name){
        return roleService.getPage(pageCurrent, pageSize, name);
    }

    /**
     * 更改角色当前状态
     * @param status 状态码 0：正常 1：异常
     * @return
     */
    @GetMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status){
        roleService.setStatus(id, status);
    }

    /**
     * 添加员工
     * @param role
     * @return
     */
    @PostMapping("/add")
    public R addRole(@RequestBody Role role) {
        //1、非空判断
        if ("".equals(role.getName())||role.getName()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if ("".equals(role.getRoleKey())||role.getRoleKey()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return roleService.addRole(role);
    }

    /**
     * 根据角色 id，删除角色
     * @param id 角色id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteRoleById(@PathVariable("id") Integer id) {
        return roleService.deleteRoleById(id);
    }

    /**
     * 根据角色id，查询单个角色信息
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public R selectOneById(@PathVariable("id") Integer id){
        return roleService.selectOneById(id);
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @PutMapping("/update")
    public R updateRole(@RequestBody Role role){
        //非空判断
        if ("".equals(role.getName())||role.getName()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if ("".equals(role.getRoleKey())||role.getRoleKey()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return roleService.updateRole(role);
    }

    /**
     * 批量删除
     * @param ids 角色 id数组
     * @return
     */
    @DeleteMapping("deleteBatch")
    public R deleteRoleBatch(@RequestBody List<Integer> ids){
        if(ids == null || ids.size() ==0 ){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return roleService.deleteRoleBatch(ids);
    }

    /**
     * 角色信息 -- 导出 Excel
     * @param response
     * @return
     */
    @GetMapping("outExcel")
    public void outExcel(HttpServletResponse response){
        roleService.outExcel(response);
    }

    /**
     * 角色信息 -- 导入 Excel
     * @param file
     * @return
     */
    @PostMapping("inExcel")
    public R inExcel(MultipartFile file)  {
        roleService.inExcel(file);
        return R.success().message("上传成功!");
    }

    /**
     * 通过角色id，获取此角色的权限 id 数组
     * @param id
     * @return
     */
    @GetMapping("getMenuByRoleId/{id}")
    public R getMenuByRoleId(@PathVariable("id") Integer id){
        return roleService.getMenuByRoleId(id);
    }

    /**
     * 为 角色添加权限
     * @param id 待添加的角色id
     * @param menuIds 添加的权限id数组
     * @return
     */
    @PostMapping("addMenuForRole/{id}")
    public R addRoleForEmployee(@PathVariable("id") Integer id, @RequestBody Integer[] menuIds){
//        System.out.println("员工id："+id);
//        for (int i = 0; i < menuIds.length; i++) {
//            System.out.println("权限id："+menuIds[i]);
//        }
        return roleService.addMenuForRole(id, menuIds);
    }
}
